<template>
  <div>
    <!--    面包屑导航区-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!--    卡片视图区-->
    <el-card>
      <!--头部搜索栏 ， 按钮区-->
      <div>
        <!--搜索框-->
        <el-input placeholder="请输入内容" v-model="page.query" style="width: 300px" clearable @clear="getGoods">
          <el-button slot="append" icon="el-icon-search" @click="getGoods"></el-button>
        </el-input>
        <!--添加按钮-->
        <el-button
            type="primary"
            style="margin-left: 20px"
            @click="$router.push('goods/addgoods')"
        >添加商品</el-button>
      </div>
      <!--中心商品区-->
      <div>
        <!--表格区-->
        <el-table :data="goodsData" border stripe>
          <el-table-column type="index"></el-table-column>
          <el-table-column label="商品名称" prop="goods_name" width="400px"></el-table-column>
          <el-table-column label="商品价格" prop="goods_price"></el-table-column>
          <el-table-column label="商品数量" prop="goods_number"></el-table-column>
          <el-table-column label="更新时间" prop="upd_time" v-slot="slot" width="200px">{{getTime(slot.row.upd_time)}}</el-table-column>
          <el-table-column label="操作" v-slot="slot" width="200px">
            <el-button type="success" @click="editGood(slot.row)" class="el-icon-setting" size="mini">编辑</el-button>
            <el-button type="danger" @click="deleteGood(slot.row)" class="el-icon-delete" size="mini">删除</el-button>
          </el-table-column>
        </el-table>
        <!--页码区-->
        <el-pagination
            @size-change="pagesizeChange"
            @current-change="pagenumChange"
            :current-page="page.pagenum"
            :page-sizes="[6, 8, 10, 12]"
            :page-size="page.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total">
        </el-pagination>
      </div>
    </el-card>

    <!--弹窗区-->
    <div>
      <!--编辑货物区-->
      <el-dialog title="编辑货物" :visible.sync="isShowEditGoods" width="500px">
        <!--表单区-->
        <el-form ref="editGoodsForm" :model="goodParams" label-width="80px">
          <el-form-item label="商品名称"><el-input v-model="goodParams.goods_name"></el-input></el-form-item>
          <el-form-item label="价格(元)"><el-input v-model="goodParams.goods_price"></el-input></el-form-item>
          <el-form-item label="重量"><el-input v-model="goodParams.goods_weight"></el-input></el-form-item>
          <el-form-item label="数量"><el-input v-model="goodParams.goods_number"></el-input></el-form-item>
        </el-form>
        <!--按钮区-->
        <span slot="footer" class="dialog-footer">
          <el-button @click="isShowEditGoods = false">取 消</el-button>
          <el-button type="primary" @click="yesEditGood">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import moment from "moment";

export default {
  name: "Goods",
  data() {
    return {
      //页码情况
      page: {
        query:'',
        pagenum:1,
        pagesize:6,
        total: 0
      },
      //当前页码商品
      goodsData: [],
      //搜索框里面的值
      searchInput:"",
      //是否显示编辑货物对话框
      isShowEditGoods:false,
      //当前这一个货物的数据
      goodParams:{}
    }
  },
  methods: {
    //获取当前页码货物
    getGoods() {
      this.$http.get('goods', {params: this.page}).then(res => {
        if (res.data.meta.status !== 200) return this.$message.error('获取列表失败')
        this.$message.success('获取列表成功')
        this.goodsData = res.data.data.goods   //赋值当前页面数据
        this.page.total = res.data.data.total  //赋值总数据个数
      })
    },
    //搜索货物
    // searchGoods() {
    //   this.$http.get('goods', {params:this.page}).then(res=>{
    //     if(res.data.meta.status!==200) return this.$message.error("获取数据失败")
    //     this.goodsData=res.data.data.goods
    //     this.page.total=res.data.data.total
    //     console.log(this.page)
    //     console.log(res)
    //   })
    // },
    //当前可展现数量发生变化
    pagesizeChange(val){
      this.page.pagesize=val
      this.getGoods()
    },
    //当前页码发生变化
    pagenumChange(val){
      this.page.pagenum=val
      this.getGoods()
    },
    //编译货物
    editGood(row){
        this.isShowEditGoods=!this.isShowEditGoods
        this.goodParams=row
    },
    //删除货物
    deleteGood(row){
      this.$confirm('确认删除？','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
        this.$http.delete('goods/'+row.goods_id).then(res=>{
          if(res.data.meta.status!==200)return this.$message.error('删除失败')
          this.$message.success('删除成功')
          this.getGoods()
        })
      }).catch(()=>{
        this.$message.info('取消删除')
      })

    },
    //确认编译货物
    yesEditGood(){
      this.$confirm('确定提交编译吗','提示',{
        type:'success'
      }).then(()=>{
        this.$http.put('goods/'+this.goodParams.goods_id,this.goodParams).then(res=>{
          console.log(this.goodParams)
          console.log(res)
          this.getGoods()
        })
      })
    },
    //格式化时间
    getTime(time){
      return moment(time).format('YYYY/MM/DD hh:mm:ss')
    }
  },
  created() {
    this.getGoods()
  }
}
</script>

<style scoped>

</style>